<?php
/**
 * The is the interface to the PhoneWars webservice. All http requests to the page
 * use POST commands to send information about which webservice call to use.
 *
 * The library requires PHP >= 5.1.2 with curl or http/https stream wrappers enabled.
 * @author Kevin M. Smith and Chris Bobo
 * @copyright Copyright (c) 2012, Kevin M. Smith and Chris Bobo
 */
require 'pw.php';

try {
	session_start();

	$pw = new PhoneWars();
	$pw->begin(session_id());

	switch($_POST['PW_COMMAND'])
	{
		case 'SET_NAME':
			print($pw->setName($_POST['name']));
			break;
		case 'GET_NAME':
			print($pw->getName());
			break;
		case 'GET_ID':
			print($pw->getId());
			break;
		case 'CREATE_MATCH_DM':
			$match_info['match_name']    = $_POST['match_name'];
			$match_info['max_players']   = $_POST['max_players'];
			$match_info['gun_radius']    = $_POST['gun_radius'];
			$match_info['gun_range']     = $_POST['gun_range'];
			$match_info['dm_max_points'] = $_POST['dm_max_points'];
			print($pw->createMatchDM($match_info));
			break;
		case 'GET_MATCHES':
			print($pw->getMatches());
			break;
		case 'GET_MATCH':
			print($pw->getMatch($_POST['match_id']));
			break;
		case 'GET_MATCH_TYPE_DEFAULTS':
			print($pw->getMatchTypeDefaults());
			break;
		case 'LOGOUT':
			$pw->logoutAccount();
			break;
		case 'JOIN_MATCH':
			print($pw->joinMatch($_POST['match_id']));
			break;
		case 'UPDATE_POSITION':
			print($pw->updatePosition($_POST['latitude'], $_POST['longitude']));
			break;
		case 'UPDATE_GUN_SHOT':
			print($pw->updateGunShot($_POST['x'], $_POST['y']));
			break;
		case 'UPDATE_MINE_LAY':
			$msg = $pw->updateMineLay();
			error_log("PW_MINE: ".$msg."\n", 3, "./pw_error.log");
			print($msg);
			break;
		case 'UPDATE_MINE_DETONATE':
			print($pw->updateMineDetonate());
			break;
		case 'UPDATE_LEAVE_MATCH':
			print($pw->updateLeaveMatch());
			break;
		case 'UPDATE_DELETE_MATCH':
			print($pw->deleteMatch($_POST['match_id']));
			break;
		default:
			throw new ErrorException('Error: Unknown command type');
			break;
	}

	$pw->end();
}
catch(ErrorException $e)
{
	$pw->end();

	$output = array('PW_ERROR' => $e->getMessage());
	print(json_encode($output));
}
?>
